package com.xianggu.designMode.combination.demo02.impl;

import com.xianggu.designMode.combination.demo02.Command;
import com.xianggu.designMode.combination.demo02.Graphic;

// 具体命令：移动图形
public class MoveCommand implements Command {
    private Graphic graphic;
    private int deltaX, deltaY;
    private int prevX, prevY; // 保存原位置用于撤销

    public MoveCommand(Graphic graphic, int deltaX, int deltaY) {
        this.graphic = graphic;
        this.deltaX = deltaX;
        this.deltaY = deltaY;
    }

    @Override
    public void execute() {
        prevX = graphic.getX();
        prevY = graphic.getY();
        graphic.move(deltaX, deltaY);
    }

    @Override
    public void undo() {
        graphic.setPosition(prevX, prevY);
    }
}
